package br.com.foxinline.servico;

import br.com.foxinline.enums.Relatorio;
import br.com.foxinline.util.Msg;
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.HashMap;
import javax.ejb.EJB;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRParameter;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.fill.JRAbstractLRUVirtualizer;
import net.sf.jasperreports.engine.fill.JRSwapFileVirtualizer;
import net.sf.jasperreports.engine.util.JRSwapFile;

/**
 *
 * @author roniere
 */
public class RelatorioServico {

    @EJB
    PrefeituraServico prefeituraServico;

    public static void genReport(HashMap<String, Object> parametros, Collection<?> collection, Relatorio relatorio) {
        try {
            if (parametros == null) {
                parametros = new HashMap<String, Object>();
            }
            JRSwapFile file = new JRSwapFile("/tmp", 4096, 10);

            JRAbstractLRUVirtualizer virtualizer = new JRSwapFileVirtualizer(10, file, true);

            parametros.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);

            FacesContext facesContext = FacesContext.getCurrentInstance();

            InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(relatorio.getUrl());

            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

            ServletOutputStream ouputStream = response.getOutputStream();

            JRDataSource jrds;
            if (collection != null) {
                jrds = new JRBeanCollectionDataSource(collection);
            } else {
                jrds = new JREmptyDataSource();
            }

            JasperRunManager.runReportToPdfStream(stream, ouputStream, parametros, jrds);

            response.setHeader("Content-Disposition", "inline; filename=\"relatorio.pdf\"");
            response.setContentType("application/pdf");
            facesContext.responseComplete();
            ouputStream.flush();
            ouputStream.close();

            virtualizer.cleanup();

        } catch (IOException e) {

            System.err.println(e);
            Msg.messagemError("Erro ao gerar relatório IOEX");

        } catch (JRException e) {

            System.err.println(e);
            Msg.messagemError("Erro ao gerar relatório JREX");
        }
    }

    public static void genReport(byte[] pdfAsBytes) {
        try {
            JRSwapFile file = new JRSwapFile("/tmp", 4096, 10);

            JRAbstractLRUVirtualizer virtualizer = new JRSwapFileVirtualizer(10, file, true);

            FacesContext facesContext = FacesContext.getCurrentInstance();
            ServletContext servletContext = (ServletContext) facesContext.getExternalContext().getContext();

            HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();

            ServletOutputStream ouputStream = response.getOutputStream();

            response.setHeader("Content-Disposition", "inline; filename=\"relatorio.pdf\"");
            response.setContentType("application/pdf");
            facesContext.responseComplete();
            ouputStream.write(pdfAsBytes);

            ouputStream.flush();
            ouputStream.close();

        } catch (IOException e) {

            System.err.println(e);
            Msg.messagemError("Erro ao gerar relatório IOEX");
        }
    }
}
